﻿using System.Collections.Generic;
using System.Windows;
using Microsoft.VisualStudio.PlatformUI;

namespace SP2010DevExtensions.ItemTemplateWizard
{
    public partial class WizardWindow : DialogWindow
    {

        public string ServiceFactoryType { get; set; }
        public string AspNetCompatibilityMode { get; set; }
        public Dictionary<string, string> ServiceFactoryOptions { get; set; }
        public Dictionary<string, string> AspNetCompatibilityModeOptions { get; set; }

        public WizardWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            ServiceFactoryOptions = new Dictionary<string, string>();
            ServiceFactoryOptions.Add("ADO.NET Data Service", "MultipleBaseAddressBasicHttpBindingServiceHostFactory");
            ServiceFactoryOptions.Add("REST Service", "MultipleBaseAddressWebServiceHostFactory");
            ServiceFactoryOptions.Add("SOAP service", "MultipleBaseAddressBasicHttpBindingServiceHostFactory");
            cmbServiceType.ItemsSource = ServiceFactoryOptions;


            AspNetCompatibilityModeOptions = new Dictionary<string, string>();
            AspNetCompatibilityModeOptions.Add("Allowed", "Allowed");
            AspNetCompatibilityModeOptions.Add("NotAllowed", "Not Allowed");
            AspNetCompatibilityModeOptions.Add("Required", "Required");
            cmbAspNetCompatibilityMode.ItemsSource = AspNetCompatibilityModeOptions;
        }

        private bool SelectionValid()
        {
           
            if(cmbServiceType.SelectedIndex==-1)
            {
                lblErrServiceType.Visibility = Visibility.Visible;
            }
            else
            {
                lblErrServiceType.Visibility = Visibility.Hidden;
            }

            if(cmbAspNetCompatibilityMode.SelectedIndex==-1)
            {
                lblErrAspNetCompatibilityMode.Visibility = Visibility.Visible;
            }
            else
            {
                lblErrAspNetCompatibilityMode.Visibility = Visibility.Hidden;  
            }
            return ((lblErrAspNetCompatibilityMode.Visibility == Visibility.Visible) || (lblErrServiceType.Visibility == Visibility.Visible));
        }

        private void finishButton_Click(object sender, RoutedEventArgs e)
        {
            if(SelectionValid())
            {
                return;
            }
            var selectedValue =(KeyValuePair<string,string>) cmbServiceType.SelectedItem;
            ServiceFactoryType = selectedValue.Value;

            selectedValue = (KeyValuePair<string, string>)cmbAspNetCompatibilityMode.SelectedItem;
            AspNetCompatibilityMode = selectedValue.Value;

            this.DialogResult = true;
            this.Close();
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
    }
}

